// 自定义 server 脚本
const { createServer } = require('http')
const { parse } = require('path')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 4000

const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
console.log(123);

app.prepare().then(() => {
  console.log(1234);
  createServer(async (req, res) => {
    try {

      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;

      if (pathname === '/info') {
        await app.render(req, res, '/', query)
      } else {
        await handle(req, res, parsedUrl)
      }

    } catch (e) {
      console.log('error', req.url, e);
      res.status = 500
      res.end('interal server error')
    }
  }).listen(port, (err) => {
    if (err) throw err
    console.log(`'server is running at 'http://${hostname}:${port}`);
  })
})